package com.example.springbootdemo.interceptor;

import com.example.springbootdemo.exception.BusinessErrorException;
import com.example.springbootdemo.bean.response.ResponseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

/**
 * 全局异常拦截器
 * @author hh
 * @date 2021/11/16
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionInterceptor {

    /**
     * 参数绑定异常
     * @param e e
     * @return ResponseInfo
     */
    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseInfo handleBindException(BindException e){
        BindingResult bindingResult = e.getBindingResult();
        String msg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
        log.error("参数校验异常拦截：{}", msg);
        return ResponseInfo.fail(msg);
    }

    /**
     * 参数格式异常处理
     * @param e exception
     * @return ResponseInfo
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseInfo handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("参数格式不合法", e);
        return ResponseInfo.fail(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }

    /**
     * 权限不足异常处理
     * @param e exception
     * @return ResponseInfo
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResponseInfo handleAccessDeniedException(AccessDeniedException e) {
        log.error("权限不足", e);
        return ResponseInfo.fail(HttpStatus.FORBIDDEN.value(), e.getMessage());
    }

    /**
     * 参数缺失异常处理
     * @param e exception
     * @return ResponseInfo
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseInfo handleMissingServletRequestParameterException(Exception e) {
        log.error("参数缺失异常", e);
        return ResponseInfo.fail(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }

    /**
     * 空指针异常
     * @param e exception
     * @return ResponseInfo
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseInfo handleNullPointerException(NullPointerException e) {
        log.error("空指针异常", e);
        return ResponseInfo.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "空指针异常");
    }

    /**
     * 拦截业务异常，返回业务异常信息
     * @param e exception
     * @return ResponseInfo
     */
    @ExceptionHandler(BusinessErrorException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseInfo handleBusinessError(BusinessErrorException e) {
        log.error("业务异常", e);
        int code = e.getCode();
        String message = e.getMessage();
        return ResponseInfo.fail(code, message);
    }

    /**
     * 系统异常处理
     * @param e exception
     * @return ResponseInfo
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseInfo handleException(Exception e) {
        log.error("系统异常", e);
        return ResponseInfo.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统发生异常，请联系管理员");
    }

    /**
     * 系统异常处理
     * @param throwable throwable
     * @return ResponseInfo
     */
    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseInfo handleThrowable(Throwable throwable) {
        log.error("系统异常", throwable);
        return ResponseInfo.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常，请联系管理员！");
    }

}
